﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.IOC.Core.Mediators;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单弹出层执行设备ViewModel
    /// </summary>
    public class ActualEquipmentsViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ActualEquipmentsViewModel(ServiceProxy<IExecutionContract> iExecutionContract, IWindowManager windowManager)
        {
            this._iExecutionContract = iExecutionContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region 属性
        #region 工单设备要求列表
        /// <summary>
        /// 工单设备要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderEquipmentAskInfo> WorkOrderEquipmentAsks { get; set; }
        #endregion

        #region 工单实际设备列表
        /// <summary>
        /// 工单实际设备列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualEquipmentInfo> WorkOrderActualEquipments { get; set; }
        #endregion

        #region 工单Id
        /// <summary>
        /// 工单Id
        /// </summary>
        [DependencyProperty]
        public Guid WorkOrderId { get; set; }
        #endregion

        #region 工厂模型Id
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInfo workOrderInfo)
        {
            WorkOrderId = workOrderInfo.Id;
            FacilityId = workOrderInfo.FacilityId;
            WorkOrderEquipmentAsks = new ObservableCollection<WorkOrderEquipmentAskInfo>(await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrderEquipmentAsks(
               workOrderInfo.Id)));
            WorkOrderActualEquipments = new ObservableCollection<WorkOrderActualEquipmentInfo>(await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrderActualEquipments(
               workOrderInfo.Id)));
        }
        #endregion

        #region 添加设备
        /// <summary>
        /// 添加设备
        /// </summary>
        public async void CreateEquipment()
        {
            AddActualEquipmentsViewModel viewModel = ResolveMediator.Resolve<AddActualEquipmentsViewModel>();
            viewModel.Load(FacilityId);
            bool? result = await this._windowManager.ShowDialogAsync(viewModel);
            if (result == true)
            {
                CreateWorkOrderActualEquipment(viewModel.SelectedEquipment.Name, viewModel.SelectedEquipment.Id, viewModel.EquipmentUse, viewModel.ActualStartTime, viewModel.ActualEndTime, viewModel.Sort, viewModel.Description);
            }
        }
        #endregion

        #region 删除工单实际设备
        /// <summary>
        /// 删除工单实际设备
        /// </summary>
        public async void RemoveWorkOrderActualPerson(WorkOrderActualEquipmentInfo workOrderActualEquipmentInfo)
        {
            this.Busy();
            //删除工单实际人员
            WorkOrderActualEquipments.Remove(workOrderActualEquipmentInfo);
            this.Idle();
        }
        #endregion

        #region 创建实际设备明细 —— async Task CreateWorkOrderActualEquipment()
        /// <summary>
        /// 创建实际设备明细
        /// </summary>
        /// <param name="personId">设备名称</param>
        /// <param name="personId">设备Id</param>
        /// <param name="personUse">设备用途</param>
        /// <param name="actualStartTime">实际开始时间</param>
        /// <param name="actualEndTime">实际结束时间</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public void CreateWorkOrderActualEquipment(string name, Guid equipmentId, string equipmentUse, DateTime actualStartTime, DateTime actualEndTime, int sort, string description)
        {
            WorkOrderActualEquipmentInfo workOrderActualEquipmentInfo = new WorkOrderActualEquipmentInfo();
            workOrderActualEquipmentInfo.EquipmentId = equipmentId;
            workOrderActualEquipmentInfo.EquipmentInfo = new EquipmentInfo() { Name = name };
            workOrderActualEquipmentInfo.EquipmentUse = equipmentUse;
            workOrderActualEquipmentInfo.ActualStartTime = actualStartTime;
            workOrderActualEquipmentInfo.ActualEndTime = actualEndTime;
            workOrderActualEquipmentInfo.Sort = sort;
            workOrderActualEquipmentInfo.Description = description;
            WorkOrderActualEquipments.Add(workOrderActualEquipmentInfo);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            #endregion

            List<WorkOrderActualEquipmentParam> workOrderActualEquipmentParams = new List<WorkOrderActualEquipmentParam>();
            foreach (var workOrderActualEquipment in WorkOrderActualEquipments)
            {
                WorkOrderActualEquipmentParam workOrderActualPersonParam = new WorkOrderActualEquipmentParam();
                workOrderActualPersonParam.equipmentId = workOrderActualEquipment.EquipmentId;
                workOrderActualPersonParam.equipmentUse = workOrderActualEquipment.EquipmentUse;
                workOrderActualPersonParam.actualStartTime = workOrderActualEquipment.ActualStartTime;
                workOrderActualPersonParam.actualEndTime = workOrderActualEquipment.ActualEndTime;
                workOrderActualPersonParam.sort = workOrderActualEquipment.Sort;
                workOrderActualPersonParam.description = workOrderActualEquipment.Description;
                workOrderActualEquipmentParams.Add(workOrderActualPersonParam);
            }
            this.Busy();
            await Task.Run(() => this._iExecutionContract.Channel.SaveWorkOrderActualEquipments(
                WorkOrderId, workOrderActualEquipmentParams));
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #endregion
    }
}
